/*
  VoIP Wholesale 1.0
  ------------------
  Document   : module_elements.js
  Created on : Sep 14, 2011, 4:16:45 PM
  Author     : Milos Jovanovic <miloscode@gmail.com>
  Description:

 */

 // simulate 
 $(function(){
	$("#button-simulate").button().click(function(){
		simulate();
	});
 });
 
 
 // filter routes
$(function(){
	
	// submit button action
	$("#filter-submit").button();
	
	// replace value of the field
	$("#dialling_code_field").click(function(){	
	
		if($(this).val() == lang_form_dialcode_begins_with)
		{
			$(this).val("");
		}
	});
	
	$('#country').change(function(){
	
	 if(numberingPlan == "external")
	 {
		load_country_routes_external(false);
	 }
	 else if(numberingPlan == "internal")
	 {
		load_country_routes_internal(false);
	 }
	 else if(numberingPlan == "special")
	 {
		load_country_routes_special(false);
	 }
		
	});
}); 
    

 
// add new entry - external numbering plan
	$(function() {
	
		$( "#dialog-form" ).dialog({
			autoOpen: false,
			height: 600,
			width: 550,
			modal: true,
			buttons: {
				"Save": function() {					
 
                                    $('#start-date').addClass('ui-state-error');
                                    
				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			},
			close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
			}
		});
		
	$( "#confirmation-box" ).dialog({
			autoOpen: false,
			height: 400,
			width: 550,
			modal: true,
			buttons: {
				"Confirm": function() {					
 
							 $('#confirm').attr('value','true');
                             save_new_route();
							 $( this ).dialog( "close" );                  
				},
				Cancel: function() {
				    $('#confirm').attr('value','false');				
					$( this ).dialog( "close" );
				}
			},
			close: function() {
				//allFields.val( "" ).removeClass( "ui-state-error" );
			}
		});		
		
	$( "#close-confirmation-box" ).dialog({
			autoOpen: false,
			height: 400,
			width: 550,
			modal: true,
			buttons: {
				"Confirm": function() {					
 
							 $('#confirm').attr('value','true');
                             close_route();
							 $( this ).dialog( "close" );                  
				},
				Cancel: function() {
				    $('#confirm').attr('value','false');				
					$( this ).dialog( "close" );
				}
			},
			close: function() {
				//allFields.val( "" ).removeClass( "ui-state-error" );
			}
		});		
		
                
// start date for opening dialcodes
$('#date-from').datepick({ 
    defaultDate: minRouteStartDate, selectDefaultDate: true,
    minDate: minRouteStartDate, dateFormat: $.datepick.ISO_8601 ,changeMonth: false});

$('#close-date').datepick({ 
    defaultDate: minRouteEndDate, selectDefaultDate: true,
    minDate: minRouteEndDate, dateFormat: $.datepick.ISO_8601 ,changeMonth: false});
	
	
// deactivate selected elements	
$('.deactivated').click(function(){
				$(this).blur();
			}).focus(function(){
				$(this).blur();
			}).select(function(){
				$(this).blur();
			}).change(function(){
				$(this).blur();
			});		

// add or reopen route			
$( "#module-save-route" )
			.button()
			.click(function() {	
                                save_new_route();                                
			});
// close route
$( "#module-close-route" )
			.button()
			.click(function() {	
                                close_route();                                
			});		

// save settings	
$( "#module-save-settings" )
			.button()
			.click(function() {	
                                save_settings();                                
			});

// common elements			
$( "#module-add-new-external" )
			.button()
			.click(function() {
				window.document.location.href = siteUrl+'/external/add';
			});
       
$( "#module-add-new-internal" )
			.button()
			.click(function() {
				window.document.location.href = siteUrl+'/internal/add';
			});

$( "#module-add-new-special" )
			.button()
			.click(function() {
				window.document.location.href = siteUrl+'/special/add';
			});

			
$( "#module-export-xls" )
			.button()
			.click(function() {
				//$( "#dialog-form" ).dialog( "open" );
			});

$( "#module-import-xls" )
			.button()
			.click(function() {
				//$( "#dialog-form" ).dialog( "open" );
			});

// options for each route in numbering plan
$( ".options").each(function(){

	$(this).button().addClass('options-button');
	
	var status = $(this).attr("rel");
	var route_id = $(this).attr("id");

	if(status == 0)
	{
		$(this).addClass('ui-state-default');
		$(this).click(function(){
			window.document.location.href = 'reopen/'+route_id;
		});
	}

	else if(status == 1)
	{
		$(this).addClass('ui-state-default');
		$(this).click(function(){
			window.document.location.href = 'close/'+route_id;
		});
	}
	
	else if(status == 2 || status == 3)
	{
		$(this).addClass('ui-state-disabled');
	}
	
	//$(this).children().append(code);

});
        });        